<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/javascript/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/javascript/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/javascript/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/javascript/images/logo.svg" color="#222">

<link rel="stylesheet" href="/javascript/css/main.css">


<link rel="stylesheet" href="/javascript/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/javascript/","scheme":"Muse","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="script脚本 属性:   type属性: text&#x2F;javascript 等 defer属性: 脚本在执行的时候不会改变页面的结构，脚本在页面解析完成后再运行。多个defer的话会按顺序，一般推荐只在一个脚本上使用defer async: 脚本异步加载，但是脚本的顺序不一定 module: 可以使用es6的import和exports模块化     在script脚本内直接写代码要避免出现&amp;l">
<meta property="og:type" content="article">
<meta property="og:title" content="javascript高级程序设计第四版">
<meta property="og:url" content="http://yoursite.com/2020/10/08/javascript%E9%AB%98%E7%BA%A7%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E7%AC%AC%E5%9B%9B%E7%89%88/index.html">
<meta property="og:site_name" content="javascript">
<meta property="og:description" content="script脚本 属性:   type属性: text&#x2F;javascript 等 defer属性: 脚本在执行的时候不会改变页面的结构，脚本在页面解析完成后再运行。多个defer的话会按顺序，一般推荐只在一个脚本上使用defer async: 脚本异步加载，但是脚本的顺序不一定 module: 可以使用es6的import和exports模块化     在script脚本内直接写代码要避免出现&amp;l">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-10-08T15:09:29.591Z">
<meta property="article:modified_time" content="2020-10-18T15:12:45.832Z">
<meta property="article:author" content="HC">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/10/08/javascript%E9%AB%98%E7%BA%A7%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E7%AC%AC%E5%9B%9B%E7%89%88/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>javascript高级程序设计第四版 | javascript</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/javascript/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">javascript</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/javascript/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/javascript/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="en">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/10/08/javascript%E9%AB%98%E7%BA%A7%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E7%AC%AC%E5%9B%9B%E7%89%88/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/javascript/images/avatar.gif">
      <meta itemprop="name" content="HC">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="javascript">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          javascript高级程序设计第四版
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-10-08 23:09:29" itemprop="dateCreated datePublished" datetime="2020-10-08T23:09:29+08:00">2020-10-08</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-10-18 23:12:45" itemprop="dateModified" datetime="2020-10-18T23:12:45+08:00">2020-10-18</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="script脚本"><a href="#script脚本" class="headerlink" title="script脚本"></a>script脚本</h2><ul>
<li><p>属性: </p>
<ul>
<li>type属性: text/javascript 等</li>
<li>defer属性: 脚本在执行的时候不会改变页面的结构，脚本在页面解析完成后再运行。多个defer的话会按顺序，一般推荐只在一个脚本上使用defer</li>
<li>async: 脚本异步加载，但是脚本的顺序不一定</li>
<li>module: 可以使用es6的import和exports模块化</li>
</ul>
</li>
</ul>
<ul>
<li>在script脚本内直接写代码要避免出现<code>&lt;/script&gt;</code>,实在想要这样的话，需要加转义字符\ ，即<code>&lt;\/script&gt;</code> </li>
</ul>
<h2 id="noscript标签"><a href="#noscript标签" class="headerlink" title="noscript标签"></a>noscript标签</h2><p>再下列情况下，会展示noscript标签的内容</p>
<ul>
<li>浏览器不支持脚本</li>
<li>浏览器对脚本的支持被关闭</li>
</ul>
<h2 id="javaScript语法细节"><a href="#javaScript语法细节" class="headerlink" title="javaScript语法细节"></a>javaScript语法细节</h2><ul>
<li>变量等的开头必须是<code>字母 _ $</code>。其他的可以是数字，字母，下划线</li>
</ul>
<h2 id="js中求余和求模的算法"><a href="#js中求余和求模的算法" class="headerlink" title="js中求余和求模的算法"></a>js中求余和求模的算法</h2><p><strong>js中 <code>%</code>主要是求余</strong></p>
<p>两者都是通过 <code>target = x - (n * y)</code>, 其中他们的商，即<code>n</code>的结果的取整有所不同</p>
<ul>
<li><p>求余：x / y 的商的结果，余数进行向0取整</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 例如 x: 10; y: 3;  求x对y的余数</span></span><br><span class="line"><span class="comment">// 1 求n</span></span><br><span class="line">x / y ≈ <span class="number">3.333333</span>; <span class="comment">//向0取整，得到3,即n为3</span></span><br><span class="line">target = x - (n * y) = <span class="number">10</span> - (<span class="number">3</span> * <span class="number">3</span>) = <span class="number">1</span></span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>求模: x / y 的商的结果，余数进行向下取整</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 例如 x: 10; y: 3;  求x对y的余数</span></span><br><span class="line"><span class="comment">// 1 求n</span></span><br><span class="line">x / y ≈ <span class="number">3.333333</span>; <span class="comment">//向下取整，得到3,即n为3</span></span><br><span class="line">target = x - (n * y) = <span class="number">10</span> - (<span class="number">3</span> * <span class="number">3</span>) = <span class="number">1</span></span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>两者的区别主要是在商的结果为负数的情况，因为整数情况下他们的取整算法都得到一样的结果</p>
<p><strong>求余的结果的正负由被除数决定(上式的x), 求模的正负由除数决定(上式的y)</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// x: 10; y: -3; n = -3.333...</span></span><br><span class="line">求余: target = <span class="number">10</span> - (<span class="number">-3</span> * <span class="number">-3</span>) = <span class="number">1</span></span><br><span class="line">求模: target = <span class="number">10</span> - (<span class="number">-3</span> * <span class="number">-4</span>) = <span class="number">-2</span></span><br><span class="line"><span class="comment">/* ------------------------------- */</span></span><br><span class="line"><span class="comment">// x: -10; y: 3; n = -3.3333...</span></span><br><span class="line">求余: target = <span class="number">-10</span> - (<span class="number">3</span> * <span class="number">-3</span>) = <span class="number">-1</span></span><br><span class="line">求模: target = <span class="number">-10</span> - (<span class="number">3</span> * <span class="number">-4</span>) = <span class="number">2</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="ajax"><a href="#ajax" class="headerlink" title="ajax"></a>ajax</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> xhr = <span class="keyword">new</span> XMLHttpRequest();</span><br></pre></td></tr></table></figure>

<h3 id="常用的方法"><a href="#常用的方法" class="headerlink" title="常用的方法"></a>常用的方法</h3><ul>
<li><p>xhr.open(请求方式, 请求url, 是否异步请求);</p>
</li>
<li><p>xhr.send(请求体); 如果是get请求，一般为null; post请求就传入相关的body</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">xhr.onreadystatechange = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (xhr.readyState === <span class="number">4</span>) &#123;</span><br><span class="line">		<span class="keyword">if</span> ((xhr.status &gt;= <span class="number">200</span> &amp;&amp; xhr.status &lt; <span class="number">300</span>) || xhr.status === <span class="number">304</span>) &#123;</span><br><span class="line">			<span class="comment">// 请求成功</span></span><br><span class="line">            <span class="built_in">console</span>.log(xhr.resonposeText);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 或者可以使用</span></span><br><span class="line">xhr.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(xhr.responseText);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item"></div>
      <div class="post-nav-item">
    <a href="/javascript/2020/10/08/%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84javascript/" rel="next" title="你不知道的javascript">
      你不知道的javascript <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#script脚本"><span class="nav-number">1.</span> <span class="nav-text">script脚本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#noscript标签"><span class="nav-number">2.</span> <span class="nav-text">noscript标签</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#javaScript语法细节"><span class="nav-number">3.</span> <span class="nav-text">javaScript语法细节</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#js中求余和求模的算法"><span class="nav-number">4.</span> <span class="nav-text">js中求余和求模的算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ajax"><span class="nav-number">5.</span> <span class="nav-text">ajax</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#常用的方法"><span class="nav-number">5.1.</span> <span class="nav-text">常用的方法</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">HC</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/javascript/archives/">
        
          <span class="site-state-item-count">12</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HC</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> v4.2.1
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">Theme – <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.7.2
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/javascript/lib/anime.min.js"></script>
  <script src="/javascript/lib/velocity/velocity.min.js"></script>
  <script src="/javascript/lib/velocity/velocity.ui.min.js"></script>

<script src="/javascript/js/utils.js"></script>

<script src="/javascript/js/motion.js"></script>


<script src="/javascript/js/schemes/muse.js"></script>


<script src="/javascript/js/next-boot.js"></script>




  















  

  

</body>
</html>
